package com.ruoyi.system.service.impl.repair;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.common.VehicleRepairConstant;
import com.ruoyi.system.common.VehicleStateConstant;
import com.ruoyi.system.domain.db.basic.BasicMaterials;
import com.ruoyi.system.domain.db.basic.BasicVehicles;
import com.ruoyi.system.domain.db.repair.VehicleRepair;
import com.ruoyi.system.domain.db.trade.TradeMaterialUsage;
import com.ruoyi.system.domain.dto.MaterialStoreDto;
import com.ruoyi.system.domain.dto.MaterialsQuantityDto;
import com.ruoyi.system.domain.dto.RepairAccendantDto;
import com.ruoyi.system.domain.dto.RepairDriverDto;
import com.ruoyi.system.domain.vo.repair.RepairAccendantVo;
import com.ruoyi.system.domain.vo.repair.RepairDriverVo;
import com.ruoyi.system.mapper.basic.BasicMaterialsMapper;
import com.ruoyi.system.mapper.basic.BasicVehiclesMapper;
import com.ruoyi.system.mapper.repair.VehicleFixMapper;
import com.ruoyi.system.mapper.repair.VehicleRepairMapper;
import com.ruoyi.system.mapper.trade.TradeMaterialUsageMapper;
import com.ruoyi.system.service.IVehicleFixService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
public class VehicleFixServiceImpl implements IVehicleFixService {

    @Resource
    private VehicleFixMapper vehicleFixMapper;
    @Resource
    private TradeMaterialUsageMapper tradeMaterialUsageMapper;
    @Resource
    private VehicleRepairMapper vehicleRepairMapper;
    @Resource
    private BasicMaterialsMapper basicMaterialsMapper;

    /**
     * 查询维修申请
     *
     * @param repairId 维修申请主键
     * @return 维修申请
     */
    @Override
    public RepairAccendantVo selectVehicleFixByRepairId(Long repairId) {
        RepairAccendantDto repairAccendantDto = vehicleFixMapper.selectVehicleFixByRepairId(repairId);
        if (repairAccendantDto != null)
        {
            return dtoToVo(repairAccendantDto);
        }
        return null;
    }

    /**
     * 查询维修申请列表
     *
     * @param vehicleRepair 维修申请
     * @return 维修申请集合
     */
    @Override
    public List<RepairAccendantVo> selectVehicleFixList(VehicleRepair vehicleRepair) {
        List<RepairAccendantDto> repairAccendantDtos = vehicleFixMapper.selectVehicleRepairList(vehicleRepair);
        if (repairAccendantDtos != null)
        {
            return repairAccendantDtos.stream().map(this::dtoToVo).collect(Collectors.toList());
        }
        return null;
    }

    /**
     * 接下维修申请
     *
     * @param vehicleRepair 维修申请
     * @return 结果
     */
    @Override
    public int updateVehicleFix(VehicleRepair vehicleRepair) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Date nowDate = DateUtils.getNowDate();
        vehicleRepair.setUpdateBy(loginUser.getUser().getUserId());
        vehicleRepair.setUpdateTime(nowDate);
        vehicleRepair.setRepairBy(loginUser.getUser().getUserId());
        return vehicleRepairMapper.updateVehicleRepair(vehicleRepair);
    }

//    /**
//     * 批量完成维修申请
//     *
//     * @param repairIds 完成的报修申请主键集合
//     * @return 结果
//     */
//    @Override
//    public int finishVehicleFixByRepairIds(Long[] repairIds) {
//        return vehicleFixMapper.up(repairIds);
//    }

    /**
     * 完成维修申请信息
     *
     * @param vehicleRepair 维修申请，利用其主键进行修改
     * @return 结果
     */
    @Override
    public int finishVehicleFixByKey(VehicleRepair vehicleRepair) {
//        basicVehiclesMapper.updateBasicVehicles(vehicleRepair.get)
        return updateVehicleFix(vehicleRepair);
    }

    /**
     * 维修过程中使用的材料添加
     */
    @Override
    public int insertUseMaterialsDuringFix(List<MaterialsQuantityDto> materials) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Date nowDate = DateUtils.getNowDate();
        int count = vehicleFixMapper.insertRepairMaterials(materials);
        materials.forEach(material->{
            TradeMaterialUsage tradeMaterialUsage = materialsQuantityDtoTotradeMaterialUsage(material);
            tradeMaterialUsage.setCreateBy(loginUser.getUser().getUserId());
            tradeMaterialUsage.setCreateTime(nowDate);
            tradeMaterialUsageMapper.insertTradeMaterialUsage(tradeMaterialUsage);
        });
        return count;
    }


    @Override
    public List<MaterialStoreDto> selectAllMaterialStore(BasicMaterials basicMaterials) {
        return tradeMaterialUsageMapper.selectAllMaterialStore(basicMaterials);
    }

    @Override
    public List<BasicMaterials> selectAllMaterials() {
        List<BasicMaterials> basicMaterials = basicMaterialsMapper.selectAllBasicMaterials();
        return basicMaterials;
    }

    public RepairAccendantVo dtoToVo(RepairAccendantDto dto)
    {
        RepairAccendantVo vo = new RepairAccendantVo();
        vo.setRepairId(dto.getRepairId());
        vo.setReason(dto.getReason());
        vo.setCreateTime(dto.getCreateTime());
        vo.setUpdateTime(dto.getUpdateTime());
        vo.setState(dto.getState());
        vo.setPhone(dto.getPhone());
        vo.setVehicleId(dto.getBasicVehicles().getVehicleId());
        vo.setVehicleNumber(dto.getBasicVehicles().getVehicleNumber());
        vo.setManufacturer(dto.getBasicVehicles().getManufacturer());
        vo.setModel(dto.getBasicVehicles().getModel());
        vo.setCapacity(dto.getBasicVehicles().getCapacity());
        if(dto.getRepairUser()!=null)
        {
            vo.setUserId(dto.getRepairUser().getUserId());
            vo.setUserName(dto.getRepairUser().getUserName());
            vo.setMaterials(dto.getMaterialUseList());
        }
        return vo;
    }

    private TradeMaterialUsage materialsQuantityDtoTotradeMaterialUsage(MaterialsQuantityDto materialsQuantityDto){
        TradeMaterialUsage result = new TradeMaterialUsage();
        result.setQuantity(materialsQuantityDto.getQuantity());
        result.setMaterialId(materialsQuantityDto.getMaterials().getMaterialId());
        return result;
    }
}
